/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/model/universe_generator.h"
#include "PlanetWars2dRT-core/specific/model/universe_planets.h"
#include "PlanetWars2dRT-core/specific/model/players.h"


EXTERN_C_BEGIN


void
universe_add_random_planets_of_player(struct universe_t* a_universe,
				      unsigned short nb,
				      unsigned char player_index)
{
  struct planet_t a_planet;
  planet_init_first_time_only_default(&a_planet);
  a_planet.nb_updates_before_next_production = 0;
  a_planet.player_index = player_index;

  size_t i = 0;
  while(i < nb)
    {
      a_planet.position.x = rand() % a_universe->size.x;
      a_planet.position.y = rand() % a_universe->size.y;
      if(universe_add_planet_unsafe(a_universe, &a_planet))
	{
	  ++i;
	}
    }
}

void
universe_add_random_neutral_planets(struct universe_t* a_universe,
				    unsigned short nb)
{
  universe_add_random_planets_of_player(a_universe,
					nb,
					NULL_PLAYER_INDEX);
}


void
universe_give_rand_neutral_planet_to_player(struct universe_t* a_universe,
					    unsigned char player_index)
{
  while(true)
    {
      size_t rand_value = rand() % universe_get_nb_planets(a_universe);
      planet_t* a_planet = universe_get_planet_no_check(a_universe,
						      rand_value);
      if(a_planet->player_index == NULL_PLAYER_INDEX)
	{
	  a_planet->player_index = player_index;
	  return;
	}
    }
}

void
universe_give_rand_neutral_planet_to_rand_true_player(struct universe_t* a_universe,
						      unsigned char nb_players)
{
  universe_give_rand_neutral_planet_to_player(a_universe,
					      rand() % nb_players);
}

void
universe_give_neutral_planet_to_true_players(struct universe_t* a_universe,
					     unsigned char nb_players)
{
  for(unsigned char i=0; i < nb_players; ++i)
    {
      universe_give_rand_neutral_planet_to_player(a_universe, i);
    }
}


struct universe_t
universe_generate_very_simple(size_2d_uint size,
			      unsigned short nb_planets)
{
  struct universe_t a_universe;
  universe_init_first_time_only(&a_universe);
  a_universe.size = size;
  universe_add_random_neutral_planets(&a_universe, nb_planets);
  return a_universe;
}

struct universe_t
universe_generate_very_simple_with_a_planet_per_player(size_2d_uint size,
						       unsigned short nb_planets,
						       unsigned char nb_players)
{
  struct universe_t a_universe;
  a_universe = universe_generate_very_simple(size, nb_planets);
  universe_give_neutral_planet_to_true_players(&a_universe, nb_players);
  return a_universe;
}


EXTERN_C_END
